a = {}  -- new array
for i = 1, 1000 do
   a[i] = 0
end

print(#a)

-- create an array with indices from -5 to 5
a = {}
for i = -5, 5 do
   a[i] = 0
end

-- matrices and multi-dimentional arrays
mt = {}  -- create the matrix
for i = 1, N do
   mt[i] = {}  -- create a new row
   for j = 1, M do
	  mt[i][j] = 0
   end
end

-- another way to create matrix
mt = {}
for i = 1, N do
   for j = 1, M do
	  mt[(i-1)*M + j] = 0
   end
end

function mult (a, rowindex, k)
   local row = a[rowindex]
   for i, v in pairs(row) do
	  row[i] = v * k
   end
end

-- linked lists

list = nil
list = {next = list, value = v}
-- traverse the list
local l = list
while l do
	  <visit l.value>
	  l = l.next
end

-- Queues and double queues
function ListNew ()
   return {first = 0, last = -1}
end

-- to avoid polluting the global space,
-- we will define all list operations inside a table,
List = {}
function List.new ()
   return {first = 0, last = -1}
end

-- now, we can insert or remove an element at both ends
function List.pushfirst (list, value)
   local first = list.first - 1
   list.first = first
   list[first] = value
end

function List.pushlast (list, value)
   local last = list.last + 1
   list.last = last
   list[last] = value
end

function List.popfirst (list)
   local first = list.first
   if first > list.last then
	  error("list is empyt")
   end
   local value = list[first]
   list[first] = nil  -- to allow garbage collection
   list.first = first + 1
   return value
end

function List.poplast (list)
   local last = list.last
   if list.first > last then error("list is empty") end
   local value = list[last]
   list[last] = nil  -- to allow garbage collection
   list.last = last - 1
   return value
end

-- sets and bags
reserved = {
   ["while"] = true,
   ["end"] = true,
   ["function"] = true,
   ["local"] = true,
}

for w in all_words() do
   if not reserved[w] then
	  --<do something with 'w'>--  -- 'w' is not reserved word
   end
end

function Set (list)
   local set = {}
   for _, l in ipairs(list) do set[l] = true end
   return set
end

reserved = Set{"while", "end", "function", "local",}

function insert (bag, element)
   bag[element] = (bag[element] or 0) + 1
end

function remove (bag, element)
   local count = bag[element]
   bag[element] = (count and count > 1) and count - 1 or nil
end

-- string buffers
locat t = {}
for line in io.lines() do
   t[#t+1] = line .. "\n"
end
local s = table.concat(t)
